package oneD.t3dviewer;

import java.io.File;
import java.io.FileInputStream;

import javax.swing.JFrame;

/**
 * Provides for a stand-alone (app rather than applet) instance of the
 * tensegrity viewer. It builds on the Applet overriding in the few situations
 * where different facilities are required.
 */
class TViewJ3DPgm extends TViewJ3D {
	String m_model_dir;

	/**
	 * Create a tensegrity viewer instance.
	 * 
	 * @param model_dir
	 *            name of directory where model data files and text file
	 *            containing label-filename list can be found.
	 * @see #LABEL_FILENAME
	 */
	public TViewJ3DPgm(String model_dir) {
		m_model_dir = model_dir;
	}

	/**
	 * Override the design for Applet with something that gets streams from
	 * Files rather than URLs.
	 */
	public void init() {
		FileInputStream is = null;
		File label_file = new File(m_model_dir + File.separator
				+ LABEL_FILENAME);
		try {
			is = new FileInputStream(label_file);
		} catch (Exception e) {
			userMessage("init: " + e.toString());
		}
		super.init(is);
	}

	/**
	 * Create data stream for specified model and send to m_threed. Stand-alone
	 * app version.
	 * 
	 * @param model_name
	 *            unqualified name of file containing model data
	 */
	public void load(String model_name) {
		FileInputStream is = null;
		File model_file = new File(m_model_dir + File.separator + model_name);
		try {
			is = new FileInputStream(model_file);
		} catch (Exception e) {
			m_threed.reset();
			userMessage(e.toString());
			return;
		}
		if (is != null)
			try {
				m_threed.load(is);
				is.close();
			} catch (Exception e) {
				userMessage(e.toString());
			}
	}

	/**
	 * Requires the path to the directory containing the model data as an
	 * argument.
	 */
	public static void main(String args[]) {
		if (args.length != 1) {
			System.out.println("Usage: TViewJ3DPgm MODEL_DIR");
			System.exit(1);
		}
		JFrame f = new JFrame("Tensegrity Viewer");
		TViewJ3D viewer = new TViewJ3DPgm(args[0]);
		viewer.init();
		f.getContentPane().add(viewer);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(INITIAL_DIM, INITIAL_DIM);
		f.setVisible(true);
		viewer.start();
	}
}
